<?php

namespace app\admin\controller;

use app\admin\service\admin\ConfigService;
use app\common\service\SystemService;
use think\exception\ValidateException;
use think\facade\App;
use think\facade\Db;

class Dashboard extends Admin
{

    public function index()
    {
        $data = ConfigService::getConfigArray('system');
        return $this->success(['data'=>$data]);
    }
    /**
     * 获取系统信息
     * @return  \think\response\Json
     */
    public function getSystemInfo(): \think\response\Json
    {
        $systemService = SystemService::getSystemInfo();
        $data = [
            ['label' => 'PHP版本', 'value' => $systemService['php']['version'],],
            ['label' => '操作系统类型', 'value' => $systemService['os']['name'] . ' ' . $systemService['os']['version']],
            ['label' => '数据库类型', 'value' => env('DB_TYPE', 'mysql'),],
            ['label' => 'ThinkPHP版本', 'value' => $systemService['thinkphp']['version']],
            ['label' => '使用内存', 'value' => ($systemService['memory']['current_usage']) / 1024 . 'MB',],
            ['label' => '服务器时区', 'value' => date_default_timezone_get()],
            ['label' => '服务端软件', 'value' => $systemService['php']['server']['SERVER_SOFTWARE']],
        ];
        return $this->success(['data' => $data]);
    }

    /**
     * 登录统计
     * @return \think\response\Json
     * @IgnorePermission
     */
    public function getLoginStatistics(): \think\response\Json
    {
        $totalCount = Db::name('admin_log')
            ->where('type', 1)
            ->cache(true, 600)
            ->count();
        $dailyCount = Db::name('admin_log')
            ->where('type', 1)
            ->whereDay('create_time')
            ->cache(true, 600)
            ->count();
        return $this->success(['data' => ['totalCount' => $totalCount, 'dailyCount' => $dailyCount]]);
    }

    /**
     * 操作统计
     * @return \think\response\Json
     * @IgnorePermission
     */
    public function getActionStatistics(): \think\response\Json
    {
        $totalCount = Db::name('admin_log')
            ->where('type', 2)
            ->cache(true, 600)
            ->count();
        $dailyCount = Db::name('admin_log')
            ->where('type', 2)
            ->whereDay('create_time')
            ->cache(true, 600)
            ->count();
        return $this->success(['data' => ['totalCount' => $totalCount, 'dailyCount' => $dailyCount]]);
    }

    /**
     * @return \think\response\Json
     * @IgnorePermission
     */
    public function getLoginChartData(): \think\response\Json
    {
        $days = [];
        for ($i = 29; $i >= 0; $i--) {
            $days[] = date('m.d', strtotime("-$i days"));
        }
        // 查询最近30天的登录数据
        $loginData = Db::name('admin_log')
            ->where('type', 1)
            ->whereTime('create_time', 'between', [date('Y-m-d', strtotime('-29 days')), date('Y-m-d')])
            ->field("DATE_FORMAT(create_time, '%m.%d') AS days, COUNT(id) AS amount")
            ->group('days')
            ->select()
            ->column('amount', 'days'); // 将days作为键，amount作为值

        $result = [
            'x' => $days,
            'y' => []
        ];
        // 填充数据，没有登录的日期填充0
        foreach ($days as $day) {
            $result['y'][] = $loginData[$day] ?? 0;
        }

        return $this->success(['data' => $result]);
    }

    /**
     * @return \think\response\Json
     * @IgnorePermission
     */
    public function getActionChartData(): \think\response\Json
    {
        $days = [];
        for ($i = 29; $i >= 0; $i--) {
            $days[] = date('m.d', strtotime("-$i days"));
        }
        // 查询最近30天的登录数据
        $loginData = Db::name('admin_log')
            ->where('type', 2)
            ->whereTime('create_time', 'between', [date('Y-m-d', strtotime('-29 days')), date('Y-m-d')])
            ->field("DATE_FORMAT(create_time, '%m.%d') AS days, COUNT(id) AS amount")
            ->group('days')
            ->select()
            ->column('amount', 'days'); // 将days作为键，amount作为值

        $result = [
            'x' => $days,
            'y' => []
        ];
        // 填充数据，没有登录的日期填充0
        foreach ($days as $day) {
            $result['y'][] = $loginData[$day] ?? 0;
        }
        return $this->success(['data' => $result]);
    }
}